import re

import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

option = Options()
option.add_argument("--headless")
option.add_argument("--disable-gpu")
service = Service(r"F:\桌面\win_r\浏览器驱动\chrome/chromedriver.exe")
driver = webdriver.Chrome(options=option, service=service)
file = open("F:/桌面/章节3.txt", encoding="utf-8")
list = file.readlines()
namelist = namelist.readlines()
file.close()
for i in range(len(list)):
    page = 1
    url = "https://w.linovelib.com" + list[i]
    driver.get(url)
    texts = driver.find_elements(By.TAG_NAME, "p")
    fileName = "F:/桌面/无职转生/" + namelist[i].replace("\n", "") + ".txt"
    print(fileName)
    file = open(fileName, "a", encoding="utf-8")

    for j in texts:
        file.write(j.text + "\n")
    while True:
        text = driver.find_element(By.ID, "footlink").find_elements(By.TAG_NAME, "a")[3].text
        if text == "下一页":
            page= page+1
            strs = url.split(".html")
            url1 = strs[0] + "_" + str(page) + ".html"
            print(url1)
            driver.get(url1)
            texts = driver.find_elements(By.TAG_NAME, "p")
            for j in texts:
                file.write(j.text + "\n")
        else:
            break

    file.close()
